package me.obstsalat.guildera;

import org.bukkit.entity.Player;
import org.bukkit.event.inventory.InventoryCloseEvent;
import org.bukkit.event.player.PlayerItemHeldEvent;
import org.bukkit.event.player.PlayerJoinEvent;
import org.bukkit.event.player.PlayerPickupItemEvent;
import org.bukkit.inventory.ItemStack;
import org.bukkit.util.Vector;

public class g_tool_res {

	private static guildera plugin;
	
	g_tool_res(guildera pl) {
		plugin = pl;
	}
	
	public boolean onPlayerJoin (PlayerJoinEvent event) {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		ToolCheck(event.getPlayer(),-1);
		return false;
	}
	
	public boolean onPlayerItemHeld(PlayerItemHeldEvent event) { 
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		ToolCheck(event.getPlayer(), event.getNewSlot());
		return false;
	}
	
	public boolean onPlayerPickupItem(PlayerPickupItemEvent event) {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
		event.setCancelled(true);
		boolean restrict = ItemCheck(event.getPlayer(), ( event.getItem().getItemStack().getTypeId()), true);
		if(restrict) {
			int p = -1;
			for(int p2 = 0; p2 < event.getPlayer().getInventory().getContents().length; p2++) {
				if(event.getPlayer().getInventory().getContents()[p2] == null && p2 != event.getPlayer().getInventory().getHeldItemSlot()) {
					p = p2;
				}
			}
			if(p > -1) {
				event.getItem().remove();
				event.getPlayer().getInventory().setItem(p, event.getItem().getItemStack());
			} else {
				double dir1 = Math.random();
				if(dir1 > 0.5) {
					dir1 = 0.5;
				} else {
					dir1 = -0.5;
				}
				double dir2 = Math.random();
				if(dir2 > 0.5) {
					dir2 = 0.5;
				} else {
					dir2 = -0.5;
				}
				event.getItem().setVelocity(new Vector(Math.random()*dir1,0,Math.random()*dir2));
			}
		} else {
			event.setCancelled(false);
		}
		return event.isCancelled();
	}
	
    public boolean onInventoryClose(InventoryCloseEvent event)
    {
		if(!plugin.checkViableWorld(event.getPlayer().getWorld())) {
			return false;
		}
        ToolCheck((Player)event.getPlayer(),-1);
        return false;
    }
	
	private void ToolCheck(Player ply, int slot) {
		if(slot == -1) {
			if(ply.getInventory().getItemInHand() != null) {
					boolean restrict = ItemCheck(ply, ply.getInventory().getItemInHand().getTypeId(), false);
					if(restrict) {
						ply.getWorld().dropItem(ply.getLocation(), ply.getInventory().getItemInHand());
						ply.getInventory().setItem(ply.getInventory().getHeldItemSlot(), new ItemStack(0));
						ply.setItemInHand(new ItemStack(0));
					}
	        	
			}
		} else {
			if(ply.getInventory().getItem(slot) != null) {
				boolean restrict = ItemCheck(ply, ply.getInventory().getItem(slot).getTypeId(), false);
				if(restrict) {
					ply.getWorld().dropItem(ply.getLocation(), ply.getInventory().getItem(slot));
					ply.getInventory().setItem(slot, new ItemStack(0));
					ply.setItemInHand(new ItemStack(0));
				}
        	
		}
		}
	}
	
	private boolean ItemCheck(Player ply, int id, boolean silent) {
		boolean restrict = true;
		try {
			if(ply.isOp()) {
				silent = true;
			}
			int lvl = plugin.config.getInt("startlevel");
			for(guild g : plugin.guilds) {
				if(g.hasMember(ply.getName())) {
					lvl = g.getLevel();
				}
			}
			if(id == 268 && lvl < 5) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 5 to use this tool.");
					}
				}
			} else if(((id >= 273 && id <= 275)||( id == 346)||(id == 291)) && lvl < 15) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 15 to use this tool.");
					}
				}
			} else if((id == 261|| id == 272) && lvl < 25) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 15 to use this tool.");
					}
				}
			} else if(id >= 256 && id <= 259 && lvl < 30) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 30 to use this tool.");
					}
				}
			} else if(((id >= 325 && id <= 327)|| id == 335 || id == 292  || id == 342 || id == 343 || id == 328)&& lvl < 35) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 35 to use this tool.");
					}
				}
			} else if(id == 267 && lvl < 40) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 45 to use this tool.");
					}
				}
			} else if(((id >= 283 && id <= 286) || id == 294) && lvl < 45) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 45 to use this tool.");
					}
				}
			} else if(((id >= 277 && id <= 279)|| id == 293) && lvl < 50) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 50 to use this tool.");
					}
				}
			} else if(id == 276 && lvl < 55) {
				if(!silent) {
					if(lvl == plugin.config.getInt("startlevel")) {
						ply.sendMessage("You need to be in a guild to use this tool.");
					} else {
						ply.sendMessage("Your guild needs to be level 50 to use this tool.");
					}
				}
			} else {
				restrict = false;
			}
			
			if(ply.isOp())
			{
				restrict = false;
			}
			
			return restrict;
		} catch (Exception ex) {
			plugin.logger.info("[GuildEra] Can't keep up with server.");
		}
		return restrict;
	}
}
